package com.baidu.controller;

import com.baidu.pojo.Exhibition;
import com.baidu.pojo.Orders;
import com.baidu.pojo.Ticket;
import com.baidu.pojo.User;
import com.baidu.service.AdministratorService;
import com.baidu.service.ExhibitionService;
import com.baidu.service.TicketService;
import com.baidu.utils.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/AdminTicket")
public class AdminTicketController {
    @Autowired
    private TicketService administratorService;
    @Autowired
    private ExhibitionService exhibitionService;
    //分页查询所有门票信息
    @RequestMapping("/getAllTicket")
    public String getAllTicket(@RequestParam(defaultValue = "1") Integer currentPage,
                            @RequestParam(defaultValue = "3") Integer pageSize,
                            HttpServletRequest request,
                            HttpSession session){
            Pages pages=new Pages();
            pages.setCurrentPage(currentPage);
            pages.setPageSize(pageSize);
            int total=administratorService.count();
            pages.setTotalCount(total);
            request.setAttribute("pages",pages);
            List<Ticket> list = administratorService.getByPage(currentPage,pageSize);
            request.setAttribute("list",list);
        return "ticketList";
    }
    //添加门票
    @RequestMapping("/addTicket")
    public String addUser(Ticket ticket){
        System.out.println("-----------------addUser---------------");
        System.out.println("ticket:"+ticket);
        //执行添加
        administratorService.addTicket(ticket);
        //重定向到/user/getAllUser
        return "redirect:/AdminTicket/getAllTicket";
    }

    @RequestMapping("/toAddTicket")
    public String toAddOrders(HttpServletRequest request){
        System.out.println("---------------toAddOrders-------------------");
        List<Exhibition> e = exhibitionService.getAllExhibition();
        //将对象保存到request中
        request.setAttribute("exhibition1",e);
        System.out.println("e:"+e);
        return "ticketAdd";
    }



    //根据id删除
    @RequestMapping("/deleteTicket")
    public String deleteTicket(Integer tid){
        System.out.println("-------------deletById-------------");
        System.out.println("user:"+tid);


        //执行删除
        administratorService.deleteTicket(tid);

        //重定向请求到/user/getAllUser
        return "redirect:/AdminTicket/getAllTicket";
    }
    //进入编辑页面
    @RequestMapping("/toEditTicket")
    public String toEditOrders(Integer tid,HttpServletRequest request){
        System.out.println("---------------toEditUser-------------------");
        System.out.println("tid:"+tid);

        //根据id查询
        Ticket ticket=administratorService.getTicketByTid(tid);

        //将对象保存到request中
        request.setAttribute("ticket2",ticket);

        List<Exhibition> exx1 = exhibitionService.getAllExhibition();
        request.setAttribute("exx1",exx1);
//        List<Ticket> t = administratorService.getAllTicket();
//        //将对象保存到request中
//        request.setAttribute("ticket1",t);

        return "ticketUpdate";
    }
//    //根据id查询指定的用户信息
//    @RequestMapping("/getOrderByTid")
//    public String getUserById(Integer tid,HttpServletRequest request){
//        System.out.println("------------getUserById-------");
//        System.out.println("id:"+tid);
//
//        //根据id查询指定的用户信息
//        Orders orders=ordersService.getOrdersByOid(tid);
//
//        //将对象保存到request中
//        request.setAttribute("user",orders);
//        return "orderDetail";
//    }

    //根据id修改
    @RequestMapping("/updateTicket")
    public String updateOrders(Ticket ticket){
        System.out.println("------------updateUser--------------");
        System.out.println("ticket:"+ticket);

        //执行修改
        administratorService.updateTicket(ticket);

        return "redirect:/AdminTicket/getAllTicket";
    }

}
